package com.sqx.modules.pay.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;

/**
 * 支付宝单笔转账
 */
public class AlipayFundTransUniTransfer {

    String privateKey  = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDRAJLlE7ejuCW0VFsCht+YqWcCwv17wxsXMNLMXIAuhB2Kvxn/taGhS8Acrb6kAVmcGdw+uBmoEVz072XKtZzg5A7otlLXKys/AUUaC4vNUvJq7cdP5EoBWIzqHdAbr7jYz4IxwD2k3Y46f61GuCkf9VR694HlNMl5kYfK6VvX4SwfYFPzCGGKRo/6O50XlQ25e90YQf4Tqz3AECO+fUMCL01rnqbU42HQmYzGKrFJOueEGyPgl8cRgNQ9Wvfh6Y4fAi+f+be9QtFsfNCHUSilCony7Qv8jARTUY/d0H5knmFdfxw8crOgM5ft6ZK9LDbrKAR7hmUX1r2zn4QWJjv3AgMBAAECggEBAJtW99c3bnhK+Ast327jW/EVh7CvyMNh4hK8gRDPd54bJ/Hl06cREo7VxHYMVPOE5Vt2hRJAtyEJtrN984RUs8Ma/lHMoYm3Vi8//GI9Tq1PB0qebFNe8/pIHIQtoG3Tj2nl4Xikz6xn17x3BQzY+Af+ycmTiIIZV935YppYE6FBLONYPi+FUYzI2r7GsE4VJosNYT2poY0DERaoQ5SvxUSIFzJ3kx023VqmDbdnK5ZKlRu3kXmqPiHbK4TenzH409nuttEkQeZfT3PUpcGvlEAd5hnAdzi6v+NOgXf5wDWBlFYuMhavfa8FtNPrtB6+QGSCC2UmP5dws2m7iW/ljDECgYEA8ajBZoOpZYbBXpo6F7D88pJ/LP4Kivm76fEG6e9n+OPTqVYKREKQKgjnDuPkrBwelouiQReAVrkKttpvCcsMZ37PJysBECp4ENC1e6/sal4obm6XgE+P5rIC+iHmCGVb4jlOFriOOtxZv+A+yKVuWF1LFqnwaAGmM+9seft3bRUCgYEA3WezEcNQ5NCk3ZTuyNNzbeKBz4Ed1oX0gkUHDHrMHxLcsuGQ8zjwIu2tYaH2HpWnbK0wAWtNPHY44/FLf1ceX60wLjlw2DSS6EhBltbhyOJf3LmUs8UGKUQEonXnqfqVWL7JiZgbkcdq3hwbUm5XZnsZJekfUDtYWZzEVFkE/9sCgYEAjlnXGxkhlwGrNJvq7nu+dPGg4pL/COCVNfhaF0GEYJNr4kshnxYKsAgwe4/46v1hc1LrkCZU3kEAM7g2QnGObATB6BOn+6rYsScQULzPqdSvQmX47odvf/iKUmfXhkiWuIsDGdU7Sybz41goA6+XcYv7KG2QhOO70MoPf0eHEQUCgYEA0Ks2qywhJra6W5hNgPtBVDRh0fdH7vKrYse5//oQPc0iMnnDimiEcvb9LQJfm63CcPu9PekqjyyBK9w0Ip0yJExvF3HNuAe82a2YG5kEjbdFUBucWd38k5HmqFNldt+UOGMAQW0ZtshzIUjuOz+BojWsmv1fJNf5yeOlajXIL3cCgYAI8Y+TCqzk4GDwbhIsscSMiFZ0vSBaCeru6wjlSWlWe6zHIZG2QVjTXeyon85WFS8UHimlxkTsjCT3r5pEYUjvjHDzf+aOWbD1kkRBkymQK+wA1v81w+p2arnE1vC695KMrD8BBDjIqX6ZGP0f2wW/KIadtI0GmnTAKwGBPWrFUw==";

    /**
     * 支付宝提现
     * @param outBizNo 业务订单号，作为业务标识，需要保证唯一性
     * @param transAmount 转账金额，单位：元
     * @param participant 转账对象
     * @return
     * @throws AlipayApiException
     */
    public String alipayWithdraw (String outBizNo,String transAmount,Participant participant) throws AlipayApiException {

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
//        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        //todo 沙箱环境
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000126625873");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
//        alipayConfig.setAppCertPath("<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->");
        alipayConfig.setAppCertPath("D:\\工作\\hkpay\\appPublicCert.crt");
//        alipayConfig.setAlipayPublicCertPath("<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->");
        alipayConfig.setAlipayPublicCertPath("D:\\工作\\hkpay\\alipayPublicCert.crt");
//        alipayConfig.setRootCertPath("<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->");
        alipayConfig.setRootCertPath("D:\\工作\\hkpay\\alipayRootCert.crt");
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
//        model.setOutBizNo("201806300002");
        model.setOutBizNo(outBizNo);
        // 设置订单总金额
        model.setTransAmount(transAmount);

        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");

        // 设置业务产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置转账业务的标题
        model.setOrderTitle("提现");

        // 设置收款方信息
//        Participant payeeInfo = new Participant();
//        payeeInfo.setIdentity("ovjsbm1076@sandbox.com");
//        payeeInfo.setName("ovjsbm1076");
//        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        model.setPayeeInfo(participant);

        // 设置业务备注
        model.setRemark("用户提现");

        // 设置转账业务请求的扩展参数
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        return response.getBody();
//        if (response.isSuccess()) {
//            System.out.println("调用成功");
//            return "提现到支付宝成功";
//        } else {
//            System.out.println("调用失败");
//            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
////             String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
////             System.out.println(diagnosisUrl);
//            return "提现到支付宝失败，请联系工作人员";
//        }
    }

//    private static AlipayConfig getAlipayConfig() {
//        String privateKey  = "<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->";
//        AlipayConfig alipayConfig = new AlipayConfig();
//        alipayConfig.setPrivateKey(privateKey);
//        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
//        alipayConfig.setAppId("<-- 请填写您的AppId，例如：2019091767145019 -->");
//        alipayConfig.setCharset("UTF-8");
//        alipayConfig.setSignType("RSA2");
//        alipayConfig.setFormat("json");
//        alipayConfig.setAppCertPath("<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->");
//        alipayConfig.setAlipayPublicCertPath("<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->");
//        alipayConfig.setRootCertPath("<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->");
//        return alipayConfig;
//    }
}
